/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var aeRequest;
var JSONString;
var moneyListing;
var moneyDriver;
var moneyAdvertise;
var numberListing;
var numberDriver;
var numberAdvertise;

function createXmlHttpRequestObject() {
    var ro;
    var browser = navigator.appName;
    // Need to determine IE7 and not do this.
    if (browser == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else {
        ro = new XMLHttpRequest();
    }
    return ro;
}

function createResponseXML(textXML) {
    // code for IE
    if (window.ActiveXObject) {
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(textXML);
        return xmlDoc;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else {
        var parser = new DOMParser();
        var xmlDoc = parser.parseFromString(textXML, "text/xml");
        return xmlDoc;
    }
}

function getAllDTO(){
    var url = '/RadioCabsProject-war/StatisticServlet';
    //alert(url);
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    aeRequest.onreadystatechange = finishUpdate;
    aeRequest.send(null);
}

function finishUpdate(){
    if(aeRequest.readyState==4){
        if(aeRequest.status == 200){
            var response = createResponseXML(aeRequest.responseText);
            JSONString = JSON.parse(aeRequest.responseText);
            moneyListing = JSONString[0];
            moneyDriver = JSONString[1];
            moneyAdvertise = JSONString[2];
        }
    }
}

function TotalListingMoney(arrayInfo){
    var totallisting = 0;
    for(var i=0; i<arrayInfo.length; i++){
        totallisting = totallisting + parseInt(arrayInfo[i].money);
    }
    return totallisting;
}
function TotalDriverMoney(arrayInfo){
    var totaldriver = 0;
    for(var i=0; i<arrayInfo.length; i++){
        totaldriver = totaldriver + parseInt(arrayInfo[i].money);
    }
    return totaldriver;
}
function TotalAdvertiseMoney(arrayInfo){
    var totaladvertise = 0;
    for(var i=0; i<arrayInfo.length; i++){
        totaladvertise = totaladvertise + parseInt(arrayInfo[i].money);
    }
    return totaladvertise;
}

function drawChart() {
    var data = google.visualization.arrayToDataTable([
        ['',              'Driver', 'Listing', 'Advertise'],
        ['NumberOfUsers',  moneyDriver.length,  moneyListing.length, moneyAdvertise.legth]
        ]);

    var options = {
        title: 'Company Performance',
        hAxis: {
            title: 'Year', 
            titleTextStyle: {
                color: 'red'
            }
        }
};

//var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}

function randomString() {
    var chars = "0123456789";
    var string_length = 6;
    var randomstring = '';
    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum,rnum+1);
    }
    return randomstring;
}
String.prototype.trim = function()
{
    var trimmed = this.replace(/^\s+|\s+$/g, '') ;
    return trimmed;
};
jQuery(document).ready(function() {
    getAllDTO();
});

